const app = getApp()
import { getSupplierInfo } from '../../../api/index'

Page({
  data: {
    supplierId: '',
    detail: {},
    showContact: false,
    showQrcode: false,
    userInfo: {}
  },
  seeQrcode () {
    this.setData({ showQrcode: true })
  },
  seeContact () {
    if (!this.data.userInfo.userId) {
      wx.$nav.navigateTo(`/pages/login/index`)
      return
    }
    this.setData({ showContact: true })
  },
  closeContact () {
    this.setData({ showContact: false })
  },
  makePhone ({ currentTarget: { dataset: { item } } }) {
    wx.$pro.makePhoneCall({ phoneNumber: item.mobile })
  },
  async getDetail () {
    try {
      const { ResultSet } = await getSupplierInfo({ supplierId: this.data.supplierId })
      this.setData({ detail: ResultSet.userData })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  onLoad ({ id }) {
    const { screenHeight, safeArea: { bottom } } = wx.$pro.getSystemInfoSync()
    const userInfo = app.globalData.userInfo ? app.globalData.userInfo : {}
    this.setData({ supplierId: id, safeBottom: screenHeight - bottom, userInfo })
    this.getDetail()
  }
})